#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      mcorrea
#
# Created:     24/04/2012
# Copyright:   (c) mcorrea 2012
# Licence:     <your licence> 
#-------------------------------------------------------------------------------

class Node:
    def __init__(self, left, value, right):
        self.value = value
        self.left = left
        self.right = right

    def __repr__(self):
        return ('( %s [%s] %s )'%( repr(self.left), repr(self.value), repr(self.right) ))


class BinaryTree:
    def __init__(self):
        self.root = None

    def add(self, value):
        if self.root is None:
            self.root = Node(None, value, None)
        else:
            self.__add(self.root, value)


    def __add(self, node, value):
        if value < node.value :
            if node.left is None:
                left = Node(None, value, None)
                node.left = left
            else:
                self.__add(node.left, value)
        else:
            if node.right is None:
                right = Node(None, value, None)
                node.right = right
            else:
                self.__add(node.right, value)


    def contains(self, value):
        if self.root is None:
            return False
        else:
            return self.__contains(self.root, value)

    def __contains(self, node, value):
        if node.value == value:
            return True
        elif value < node.value:
            if node.left is None:
                return False
            else:
                return self.__contains(node.left, value)
        else:
            if node.right is None:
                return False
            else:
                return self.__contains(node.right, value)


    def __repr__(self):
        return repr(self.root)

def main():
    bst = BinaryTree()
    bst.add(5)
    bst.add(2)
    bst.add(7)
    bst.add(1)
    bst.add(15)

    print bst.contains(2)
    print bst.contains(15)
    print bst.contains(4)

    print bst

if __name__ == '__main__':
    main()
